﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KlauserswagModel;
using WebShopDAL;

namespace ProjektarbeteKlauserSwag
{
    public partial class MasterSite : System.Web.UI.MasterPage
    {
        List<OrderRow> cart;
        List<Category> categories;

        public int tal = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["CurrentUser"] == null)
                {
                    Response.Redirect("~/Login.aspx");
                }
                else
                {
                    FillMenu();
                    UpdateDropdownCartList();
                }
            }
            catch (Exception)
            {
            }
        }

        private void FillMenu()
        {
            categories = null;
            MenuLeft.Items.Clear();
            categories = CategoryDB.GetAllCategories();

            foreach (var category in categories)
            {
                MenuItem menuItem = new MenuItem(category.CategoryName.ToString(), category.CategoryID.ToString());
                MenuLeft.Items.Add(menuItem);
            }
        }

        protected void MenuLeft_MenuItemClick(object sender, MenuEventArgs e)
        {
            try
            {
                Session["CategoryID"] = new Category
                {
                    CategoryID = int.Parse(MenuLeft.SelectedItem.Value),
                    CategoryName = MenuLeft.SelectedItem.Text.ToString()
                };

                //(MenuLeft.SelectedItem.Text.ToString(), int.Parse(MenuLeft.SelectedItem.Value));

                Response.Redirect("~/ShowProducts.aspx");
            }
            catch (Exception)
            {
            }
        }

        public void UpdateDropdownCartList()
        {
            DropDownListCart.Items.Clear();
            if (Session["Order"] != null)
            {
                cart = Session["Order"] as List<OrderRow>;

                foreach (OrderRow orderRow in cart)
                {
                    Article article = ArticleDB.GetArticleByArticleID(orderRow.ArticleID);
                    DropDownListCart.Items.Add(new ListItem(article.ArticleName, article.ArticleID.ToString()));
                }
            }
        }

        protected void ButtonCheckOut_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/CartPayForm.aspx");
        }

        protected void ButtonLogOut_Click(object sender, EventArgs e)
        {
            try
            {
                Session["CurrentUser"] = null;
                Session["Order"] = null;
                Response.Redirect("~/Home.aspx");

                cart.Clear();
                categories.Clear();
            }
            catch (Exception)
            {
            }
        }
    }
}